package com.ruoyi.web.controller.ems;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeRelation;
import com.ruoyi.system.service.IEmsBarcodeRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/4/10 15:07
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Api(tags = "序列号物料追溯表api")
@RequestMapping("/ems/barcodeRelation")
public class EmsBarcodeRelationController extends BaseController {

    @Autowired
    IEmsBarcodeRelationService iEmsBarcodeRelationService;

    /**
     * 查询序列号物料追溯列表
     */
    @ApiOperation(value = "查询-序列号物料追溯列表")
    @GetMapping("/list")
    public TableDataInfo list(EmsBarcodeRelation emsBarcodeRelation) {
        startPage();
        LambdaQueryWrapper<EmsBarcodeRelation> lqw = new LambdaQueryWrapper<EmsBarcodeRelation>();
        //工单号
        if (StringUtils.isNotBlank(emsBarcodeRelation.getPo())) {
            lqw.like(EmsBarcodeRelation::getPo, emsBarcodeRelation.getPo());
        }
        //产品编码
        if (StringUtils.isNotBlank(emsBarcodeRelation.getItemCode())) {
            lqw.like(EmsBarcodeRelation::getItemCode, emsBarcodeRelation.getItemCode());
        }
        //产品条码
        if (StringUtils.isNotBlank(emsBarcodeRelation.getBarCode())) {
            lqw.like(EmsBarcodeRelation::getBarCode, emsBarcodeRelation.getBarCode());
        }
        if (StringUtils.isNotBlank(emsBarcodeRelation.getSonBarCode())) {
            lqw.like(EmsBarcodeRelation::getSonBarCode, emsBarcodeRelation.getSonBarCode());
        }
        //物料编码
        if (StringUtils.isNotBlank(emsBarcodeRelation.getSonItemCode())) {
            lqw.like(EmsBarcodeRelation::getSonItemCode, emsBarcodeRelation.getSonItemCode());
        }
        //上传标识
        if (emsBarcodeRelation.getUploadFlag() != null) {
            lqw.eq(EmsBarcodeRelation::getUploadFlag, emsBarcodeRelation.getUploadFlag());
        }
        //是否解绑
        if (emsBarcodeRelation.getSegment5() != null) {
            lqw.eq(EmsBarcodeRelation::getSegment5, emsBarcodeRelation.getSegment5());
        }else {
            lqw.isNull(EmsBarcodeRelation::getSegment5);
        }
        //采集时间
        if (null != emsBarcodeRelation.getBeginTime()) {
            lqw.apply("created_date>='" + DateTime.of(emsBarcodeRelation.getBeginTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        if (null != emsBarcodeRelation.getEndTime()) {
            lqw.apply("created_date<='" + DateTime.of(emsBarcodeRelation.getEndTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        //华为任务令
        if (StringUtils.isNotBlank(emsBarcodeRelation.getSegment3())) {
            lqw.like(EmsBarcodeRelation::getSegment3, emsBarcodeRelation.getSegment3());
        }
        lqw.orderByDesc(EmsBarcodeRelation::getCreatedDate);
        List<EmsBarcodeRelation> list = iEmsBarcodeRelationService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 新增序列号物料追溯
     */
    @Log(title = "新增序列号物料追溯", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增-序列号物料追溯")
    @PostMapping
    public AjaxResult add(@RequestBody EmsBarcodeRelation emsBarcodeRelation) {
        emsBarcodeRelation.setCreatedBy(SecurityUtils.getUsername());
        emsBarcodeRelation.setCreatedDate(new Date());
        return toAjax(iEmsBarcodeRelationService.save(emsBarcodeRelation) ? 1 : 0);
    }

    /**
     * 修改序列号物料追溯
     */
    @Log(title = "修改序列号物料追溯", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改-序列号物料追溯")
    @PutMapping
    public AjaxResult edit(@RequestBody EmsBarcodeRelation emsBarcodeRelation) {
        emsBarcodeRelation.setUpdatedBy(SecurityUtils.getUsername());
        emsBarcodeRelation.setUpdatedDate(new Date());
        return toAjax(iEmsBarcodeRelationService.updateById(emsBarcodeRelation) ? 1 : 0);
    }

    /**
     * 删除序列号物料追溯
     */
    @Log(title = "删除序列号物料追溯", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除-序列号物料追溯")
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iEmsBarcodeRelationService.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }

    @PostMapping("/unbindingRelation")
    @ApiOperation(value = "EMS_BARCODE_RELATION数据解绑")
    public AjaxResult unbindingRelation(@RequestBody List<EmsBarcodeRelation> list,Date updatedDate ) {
        if(updatedDate==null){
            updatedDate = new Date();
        }
        iEmsBarcodeRelationService.unbindingRelation(list,updatedDate);
        return AjaxResult.success("解绑成功");
    }

}
